/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.business.dao;

import han.bifang.common.entity.EntityConstants;
import han.bifang.common.entity.SoaTreeUnit;
import han.bifang.common.exception.BifangErrorCode;
import han.bifang.common.exception.BifangException;
import han.bifang.common.persist.dao.DaoManager;
import han.bifang.common.persist.tx.Tx;
import han.bifang.common.persist.tx.TxManager;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class TreeDao {
	private static Log logger = LogFactory.getLog(TreeDao.class);

	public static List getDomainRoot() throws BifangException {
		Tx tx = null;
		try {
			tx = TxManager.start();
			String sql = "from SoaTreeUnit t where t.type = '" + EntityConstants.TREE_DOMAIN + "' and t.subType in( '" + EntityConstants.TREE_DOMAIN_LOCAL + 
			"','" + EntityConstants.TREE_DOMAIN_REMOTE + "') ";
			List nodes = DaoManager.getDao().find(sql);
			tx.commit();
			return nodes;

		} catch (Throwable e) {
			logger.error("TreeDao.getDomainRoot()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"getDomainRoot() error", e);
		} finally {
			tx.end();
		}

	}
	
	public static List getRemoteDomains() throws BifangException {
		Tx tx = null;
		try {
			tx = TxManager.start();
			String sql = "from SoaTreeUnit t where t.type = '" + EntityConstants.TREE_DOMAIN + "' and t.subType  = '" +  EntityConstants.TREE_DOMAIN_REMOTE + "' ";
			List nodes = DaoManager.getDao().find(sql);
			tx.commit();
			return nodes;

		} catch (Throwable e) {
			logger.error("TreeDao.getRemoteDomains()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"getRemoteDomains() error", e);
		} finally {
			tx.end();
		}

	}
	
	public static List getAppRoot() throws BifangException {
		Tx tx = null; 
		try {
			tx = TxManager.start();
			String sql = "from SoaTreeUnit t where t.type = '" + EntityConstants.TREE_APP + 
				"' and t.subType ='" + EntityConstants.TREE_APP_APP + "' ";
			List nodes = DaoManager.getDao().find(sql);
			//logger.info("nodes " + nodes);
			tx.commit();
			return nodes;

		} catch (Throwable e) {
			logger.error("TreeDao.getAppRoot()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"getAppRoot() error", e);
		} finally {
			tx.end();
		}

	}
	
	public static List getChildren(String id) throws BifangException {
		Tx tx = null;
		try {
			tx = TxManager.start();
			String sql = "from SoaTreeUnit t where t.parentId = '" + id + "'";
			List nodes = DaoManager.getDao().find(sql);
			tx.commit();
			return nodes;

		} catch (Throwable e) {
			logger.error("TreeDao.getChildren()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"getChildren() error", e);
		} finally {
			tx.end();
		}

	}
	
	public static void createTreeUnit(SoaTreeUnit u)throws BifangException {
		Tx tx = null;
		try {
			tx = TxManager.start();
			DaoManager.getDao().save(u);
			DaoManager.getDao().flush();
			tx.commit();
			

		} catch (Throwable e) {
			logger.error("TreeDao.createTreeUnit()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"createTreeUnit() error", e);
		} finally {
			tx.end();
		}

	}
	
	public static void modifyTreeUnit(SoaTreeUnit u)throws BifangException {
		Tx tx = null;
		try {
			tx = TxManager.start();
			DaoManager.getDao().merge(u);
			DaoManager.getDao().flush();
			tx.commit();
			

		} catch (Throwable e) {
			logger.error("TreeDao.createTreeUnit()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"createTreeUnit() error", e);
		} finally {
			tx.end();
		}

	}
	
	public static void removeTreeUnit(SoaTreeUnit u)throws BifangException {
		Tx tx = null;
		try {
			tx = TxManager.start();
			List c = getChildren(u.getId());
			if(c!= null && c.size() > 0)
				throw new BifangException(BifangErrorCode.TREE_HAS_CHINDREN_ERROR,"Node has children");
			DaoManager.getDao().delete(u);
			DaoManager.getDao().flush();
			tx.commit();
			

		} catch (Throwable e) {
			logger.error("TreeDao.removeTreeUnit()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"removeTreeUnit() error", e);
		} finally {
			tx.end();
		}

	}
	
	public static void removeTreeUnit(String id)throws BifangException {
		Tx tx = null;
		try {
			tx = TxManager.start();
			List c = getChildren(id);
			if(c!= null && c.size() > 0)
				throw new BifangException(BifangErrorCode.TREE_HAS_CHINDREN_ERROR,"Node has children");
			Object o = DaoManager.getDao().get(SoaTreeUnit.class, id);
			DaoManager.getDao().delete(o);
			DaoManager.getDao().flush();
			tx.commit();
			

		} catch (Throwable e) {
			logger.error("TreeDao.removeTreeUnit(String)", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"removeTreeUnit(String) error", e);
		} finally {
			tx.end();
		}

	}
	
	public static SoaTreeUnit getTreeUnit(String id)throws BifangException {
		Tx tx = null;
		try {
			tx = TxManager.start();
			
			SoaTreeUnit o = (SoaTreeUnit)DaoManager.getDao().get(SoaTreeUnit.class, id);
			tx.commit();
			return o;
		} catch (Throwable e) {
			logger.error("TreeDao.removeTreeUnit(String)", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"removeTreeUnit(String) error", e);
		} finally {
			tx.end();
		}

	}

	
}
